package org.nstack.network.service.portforwarding;

import org.nstack.network.service.vip.VipVO;
import org.springframework.http.HttpMethod;
import org.nstack.header.identity.Action;
import org.nstack.header.message.APICreateMessage;
import org.nstack.header.message.APIEvent;
import org.nstack.header.message.APIMessage;
import org.nstack.header.message.APIParam;
import org.nstack.header.notification.ApiNotification;
import org.nstack.header.rest.RestRequest;
import org.nstack.header.vm.VmNicVO;


@Action(category = PortForwardingConstant.ACTION_CATEGORY)
@RestRequest(
        path = "/port-forwarding",
        method = HttpMethod.POST,
        responseClass = APICreatePortForwardingRuleEvent.class,
        parameterName = "params"
)
public class APICreatePortForwardingRuleMsg extends APICreateMessage {

    @APIParam(resourceType = VipVO.class, checkAccount = true, operationTarget = true)
    private String vipUuid;

    @APIParam(numberRange = {1, 65535})
    private Integer vipPortStart;

    @APIParam(required = false, numberRange = {1, 65535})
    private Integer vipPortEnd;

    @APIParam(required = false, numberRange = {1, 65535})
    private Integer privatePortStart;

    @APIParam(required = false, numberRange = {1, 65535})
    private Integer privatePortEnd;

    @APIParam(validValues = {"TCP", "UDP"})
    private String protocolType;

    @APIParam(required = false, resourceType = VmNicVO.class, operationTarget = true)
    private String vmNicUuid;

    private String allowedCidr;

    @APIParam(maxLength = 255)
    private String name;

    @APIParam(required = false, maxLength = 2048)
    private String description;

    public String getVipUuid() {
        return vipUuid;
    }

    public void setVipUuid(String vipUuid) {
        this.vipUuid = vipUuid;
    }

    public Integer getVipPortStart() {
        return vipPortStart;
    }

    public void setVipPortStart(Integer vipPortStart) {
        this.vipPortStart = vipPortStart;
    }

    public Integer getVipPortEnd() {
        return vipPortEnd;
    }

    public void setVipPortEnd(Integer vipPortEnd) {
        this.vipPortEnd = vipPortEnd;
    }

    public Integer getPrivatePortStart() {
        return privatePortStart;
    }

    public void setPrivatePortStart(Integer privatePortStart) {
        this.privatePortStart = privatePortStart;
    }

    public Integer getPrivatePortEnd() {
        return privatePortEnd;
    }

    public void setPrivatePortEnd(Integer privatePortEnd) {
        this.privatePortEnd = privatePortEnd;
    }

    public String getProtocolType() {
        return protocolType;
    }

    public void setProtocolType(String protocolType) {
        this.protocolType = protocolType;
    }

    public String getVmNicUuid() {
        return vmNicUuid;
    }

    public void setVmNicUuid(String vmNicUuid) {
        this.vmNicUuid = vmNicUuid;
    }

    public String getAllowedCidr() {
        return allowedCidr;
    }

    public void setAllowedCidr(String allowedCidr) {
        this.allowedCidr = allowedCidr;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public static APICreatePortForwardingRuleMsg __example__() {
        APICreatePortForwardingRuleMsg msg = new APICreatePortForwardingRuleMsg();
        msg.setName("pf1");
        msg.setVipPortStart(22);
        msg.setVipUuid(uuid());
        msg.setProtocolType("TCP");
        msg.setVmNicUuid(uuid());
        return msg;
    }

    public ApiNotification __notification__() {
        APIMessage that = this;

        return new ApiNotification() {
            @Override
            public void after(APIEvent evt) {
                if (evt.isSuccess()) {
                    ntfy("Created").resource(((APICreatePortForwardingRuleEvent)evt).getInventory().getUuid(),PortForwardingRuleVO.class.getSimpleName())
                            .messageAndEvent(that, evt).done();
                }
            }
        };
    }
}


